package AgentTravel.Behaviours;

import jade.core.AID;
import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.lang.acl.UnreadableException;
import jade.wrapper.StaleProxyException;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import AgentTravel.LogicObjects.User;
import AgentTravel.LogicObjects.UserInfoForServer;
import AgentTravel.Messages.LoginMessage;
import AgentTravel.Messages.RegisterMessage;
import AgentTravel.Messages.ServerConfirmationInfo;

public class ServerGetLoginBehaviour extends CyclicBehaviour 
{
	private Map<String,UserInfoForServer> users;
	private Map<String,String> passLogin;
	private Map<String,AID> userGui;
	
	public ServerGetLoginBehaviour(Map<String, UserInfoForServer> users2,Map<String, String> passLogin2,Map <String,AID> userGui)
	{
		this.users=users2;
		this.passLogin=passLogin2;
		this.userGui=userGui;
	}
	
	
	@Override
	public void action() 
	{
		MessageTemplate mt=MessageTemplate.MatchPerformative(ACLMessage.PROPOSE);
		
		ACLMessage msg = myAgent.receive(mt);
		
		if (msg != null) 
		{
			LoginMessage msgObj=null;
		
			try
			{
				msgObj=(LoginMessage)msg.getContentObject();
			}
			catch(UnreadableException ex)
			{
				System.out.println("UUUUnreadable :(");
			}
			
			
			if(msgObj!=null)
			{
				System.out.println("SERWER: Dostalem prosbe o login");
				if(users.containsKey(msgObj.UserName) && passLogin.get(msgObj.UserName).equals(msgObj.Password))
				{
					//usuwanie poprzednich wpisow z userGui
					LinkedList<String> toRemove=new LinkedList<String>();
					Iterator<String> iter=userGui.keySet().iterator();
					while(iter.hasNext())
					{
						String s=iter.next();
						if(userGui.get(s).equals(msg.getSender()))
						{
							toRemove.add(s);
						}
					}
					for(int i=0;i<toRemove.size();i++)
						userGui.remove(toRemove.get(i));
					
					userGui.put(msgObj.UserName, msg.getSender());
					// logowanie ok
					ACLMessage reply=new ACLMessage(ACLMessage.ACCEPT_PROPOSAL);
					reply.addReceiver(msg.getSender());
					try 
					{
						UserInfoForServer uifs=users.get(msgObj.UserName);
						reply.setContentObject(new ServerConfirmationInfo(
								users.get(msgObj.UserName).yourHostAgent.getName(), 
								users.get(msgObj.UserName).yourVisitorAgent.getName(), 
								users.get(msgObj.UserName).yourUserInfo));
					} 
					catch (IOException e) 
					{
						System.out.println("Nie udalo sie ustawic contentu User");
					} catch (StaleProxyException e) 
					{
						System.out.println("Stale Proxy Exception, bieda");
					}
					myAgent.send(reply);
				}
				else
				{
					// bledne logowanie
					ACLMessage reply=new ACLMessage(ACLMessage.REJECT_PROPOSAL);
					reply.addReceiver(msg.getSender());
					myAgent.send(reply);
				}
			}
			else	
			{
				System.out.println("Nie ma tu zadnego obiektu");
			}
			
			
		}
		else 
		{
			block();
		}
		
	}
}
